package cn.admobiletop.adsuyi.adapter.toutiao.loader;

import android.util.DisplayMetrics;

import com.bytedance.sdk.openadsdk.AdSlot;
import com.bytedance.sdk.openadsdk.TTAdConstant;
import com.bytedance.sdk.openadsdk.TTAdNative;

import cn.admobiletop.adsuyi.ADSuyiSdk;
import cn.admobiletop.adsuyi.ad.ADSuyiFullScreenVodAd;
import cn.admobiletop.adsuyi.ad.adapter.ADSuyiAdapterLoader;
import cn.admobiletop.adsuyi.ad.adapter.ADSuyiAdapterParams;
import cn.admobiletop.adsuyi.ad.data.ADSuyiPlatformPosId;
import cn.admobiletop.adsuyi.ad.error.ADSuyiError;
import cn.admobiletop.adsuyi.ad.listener.ADSuyiFullScreenVodAdListener;
import cn.admobiletop.adsuyi.adapter.toutiao.ADSuyiIniter;
import cn.admobiletop.adsuyi.adapter.toutiao.listener.FullScreenVodAdListener;
import cn.admobiletop.adsuyi.adapter.toutiao.manager.TTAdInitManager;
import cn.admobiletop.adsuyi.adapter.toutiao.util.TTAdUtil;
import cn.admobiletop.adsuyi.config.ADSuyiConfig;
import cn.admobiletop.adsuyi.config.ADSuyiInitConfig;
import cn.admobiletop.adsuyi.util.ADSuyiAdUtil;

/**
 * @author ciba
 * @description 全屏视频广告加载器
 * @date 2020/4/2
 */
public class FullScreenVodAdLoader implements ADSuyiAdapterLoader<ADSuyiFullScreenVodAd, ADSuyiFullScreenVodAdListener> {
    private FullScreenVodAdListener fullScreenVodAdListener;

    @Override
    public void loadAd(ADSuyiFullScreenVodAd fullScreenVodAd, ADSuyiAdapterParams adapterParams, ADSuyiFullScreenVodAdListener adListener) {
        if (!ADSuyiAdUtil.isReleased(fullScreenVodAd)
                && adapterParams != null
                && adapterParams.getPlatformPosId() != null
                && adListener != null) {
            ADSuyiPlatformPosId platformPosId = adapterParams.getPlatformPosId();

            ADSuyiInitConfig config = ADSuyiSdk.getInstance().getConfig();
            if (config != null && config.isFilterThirdQuestion()) {
                if (TTAdUtil.withoutLowSystem()) {
                    adListener.onAdFailed(ADSuyiError.createErrorDesc(ADSuyiIniter.PLATFORM, platformPosId.getPlatformPosId(), -1, "过滤FullScreen广告，经过测试头条的广告在安卓5.1及以下版本的手机上可能存在兼容性问题"));
                    return;
                }
            }

            TTAdNative ttAdNative = TTAdInitManager.getInstance().createNative(fullScreenVodAd.getActivity());
            if (ttAdNative == null) {
                adListener.onAdFailed(ADSuyiError.createErrorDesc(ADSuyiIniter.PLATFORM, platformPosId.getPlatformPosId(), -1, "头条SDK createNative失败，可能初始化失败或初始化数据有误"));
                return;
            }

            DisplayMetrics metrics = fullScreenVodAd.getActivity().getResources().getDisplayMetrics();
            int widthPixels = metrics.widthPixels;
            int heightPixels = metrics.heightPixels;
            float density = metrics.density;

            int orientation = ADSuyiConfig.ScreenOrientation.SCREEN_ORIENTATION_LANDSCAPE == platformPosId.getScreenOrientation() ? TTAdConstant.HORIZONTAL : TTAdConstant.VERTICAL;
            AdSlot adSlot = new AdSlot.Builder()
                    .setCodeId(platformPosId.getPlatformPosId())
                    .setSupportDeepLink(true)
                    .setAdCount(1)
                    .setExpressViewAcceptedSize(widthPixels / density, heightPixels / density)
                    .setImageAcceptedSize(widthPixels, heightPixels)
                    // 设置期望视频播放的方向，为TTAdConstant.HORIZONTAL或TTAdConstant.VERTICAL
                    .setOrientation(orientation)
                    .setDownloadType(TTAdUtil.getDisplayDownloadTipsDialogType())
                    .build();

            fullScreenVodAdListener = new FullScreenVodAdListener(platformPosId.getPlatformPosId(), adListener);
            ttAdNative.loadFullScreenVideoAd(adSlot, fullScreenVodAdListener);
        }
    }

    @Override
    public void onResumed() {

    }

    @Override
    public void onPaused() {

    }

    @Override
    public void release() {
        if (fullScreenVodAdListener != null) {
            fullScreenVodAdListener.release();
            fullScreenVodAdListener = null;
        }
    }
}
